package com.ruoyi.door.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.easy.query.api.proxy.client.EasyEntityQuery;
import com.ruoyi.common.core.domain.entity.MemberCard;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;

import com.ruoyi.door.controller.app.orderinfo.vo.AppOrderDetailCreateReqVO;
import com.ruoyi.door.controller.app.orderinfo.vo.AppOrderInfoCreateReqVO;
import com.ruoyi.door.controller.app.orderinfo.vo.AppOrderInfoSubmitReqVO;
import com.ruoyi.door.domain.DoorDeptPrice;
import com.ruoyi.door.domain.DoorHouse;
import com.ruoyi.door.domain.DoorOrder;
import com.ruoyi.door.service.DoorOrderService;
import com.ruoyi.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component("doorJob")
@Slf4j
public class DoorJob {
    @Autowired
    private EasyEntityQuery easyEntityQuery;
    @Autowired
    private DoorOrderService doorOrderService;

    @Autowired
    private ISysUserService sysUserService;

    public void ryNoParams() {
        System.out.println("执行无参方法");
        //查询还有5天就到期的房屋
        Date now = new Date();
        Date fiveDaysLater = DateUtils.addDays(now, 5);
        List<DoorHouse> houseList = easyEntityQuery.queryable(DoorHouse.class)
                .where(d -> {
                    d.endDate().format("yyyy-MM-dd").ge(DateUtil.format(now, "yyyy-MM-dd"));
                    d.endDate().format("yyyy-MM-dd").le(DateUtil.format(fiveDaysLater, "yyyy-MM-dd"));
                })
                .toList();
        if (CollectionUtil.isNotEmpty(houseList)) {
            //遍历要扣费的房产
            for (DoorHouse doorHouse : houseList) {
                SysUser sysUser = sysUserService.selectUserById(doorHouse.getLandlordId());
                //查找房主信息
                MemberCard memberCard = easyEntityQuery.queryable(MemberCard.class).where(m -> m.memberId().eq(doorHouse.getLandlordId()))
                        .firstOrNull();

                if (ObjectUtil.isNotNull(memberCard) && ObjectUtil.isNotNull(sysUser)) {
                    //创建订单
                    AppOrderInfoCreateReqVO createReqVO = new AppOrderInfoCreateReqVO();
                    createReqVO.setType(1L);

                    createReqVO.setProtocolNo(memberCard.getProtocolNo());
                    createReqVO.setChName(memberCard.getChName());
                    createReqVO.setChIdNo(memberCard.getChIdNo());
                    createReqVO.setChMobile(memberCard.getChMobile());
                    createReqVO.setRemark("自动续费");
                    createReqVO.setPayChannel("2");
                    AppOrderDetailCreateReqVO detail = new AppOrderDetailCreateReqVO();
                    detail.setType(1L);
                    detail.setName(sysUser.getNickName());
                    detail.setPhone(sysUser.getPhonenumber());
                    detail.setCountry(doorHouse.getCountry());
                    detail.setProvince(doorHouse.getProvince());
                    detail.setCity(doorHouse.getCity());
                    detail.setDistrict(doorHouse.getDistrict());
                    detail.setStreet(doorHouse.getStreet());
                    detail.setVillage(doorHouse.getVillage());
                    detail.setDeptId(doorHouse.getDeptId());
                    detail.setAddress(doorHouse.getAddress());
                    detail.setDoor(doorHouse.getDoor());
                    detail.setHouseId(doorHouse.getId());
                    detail.setBuyCount(1L);
                    createReqVO.getOrderDetailList().add((detail));
                    DoorOrder orderInfo = doorOrderService.add(createReqVO);
                    //发起扣款
                    AppOrderInfoSubmitReqVO submitReqVO = new AppOrderInfoSubmitReqVO();
                    submitReqVO.setOrderNo(orderInfo.getOrderNo());
                    submitReqVO.setOrderId(orderInfo.getId());
                    doorOrderService.submit(submitReqVO);
                }
            }
        }

    }
}
